package com.neusoft.resturant.service.impl;

import com.neusoft.resturant.common.exception.ServiceException;
import com.neusoft.resturant.entity.Address;
import com.neusoft.resturant.mapper.AddressMapper;
import com.neusoft.resturant.service.IAddressService;
import com.neusoft.resturant.service.IDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 新增收货地址的实现类
 */
@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;
    //在添加用户的收货地址的业务层依赖于IDistrictService的业务层接口
    @Autowired
    private IDistrictService districtService;

    private static final Integer maxCount = 20;

    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        //调用收货地址统计的方法
        Integer count = addressMapper.countByUid(uid);
        if (count >= maxCount) {
            throw new ServiceException("用户收货地址超出限制");
        }
        //数据补全（省市区）
        String ProvinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName = districtService.getNameByCode(address.getCityCode());
        String areaName = districtService.getNameByCode(address.getAreaCode());
        address.setProvinceName(ProvinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);
        //uid isDelete
        address.setUid(uid);
        address.setIsDefault(count == 0 ? 1 : 0);
        //补全日制
        address.setCreatedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());
        //插入收获地址的方法；
        Integer rows = addressMapper.insert(address);
        if (rows != 1) {
            throw new ServiceException("插入异常");
        }
    }

    @Override
    public List<Address> getByUidAddress(Integer uid) {
        List<Address> uidAddresses = addressMapper.findByUidAddresses(uid);
        Iterator<Address> iterator = uidAddresses.iterator();
        while (iterator.hasNext()) {
            Address test = iterator.next();
//            test.setAid(null);
//            test.setUid(null);
//            test.setProvinceName(null);
            test.setProvinceCode(null);
            test.setCityCode(null);
//            test.setCityName(null);
//            test.setAreaName(null);
            test.setAreaCode(null);
//            test.setZip(null);
//            test.setPhone(null);
            test.setTel(null);
            test.setCreatedTime(null);
            test.setCreatedUser(null);
//            test.setIsDefault(null);
            test.setModifiedTime(null);
            test.setModifiedUser(null);
        }
        return uidAddresses;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address byAidAddress = addressMapper.findByAidAddress(aid);
        if (byAidAddress == null) {
            throw new ServiceException("用户地址没有找到");
        }
        //判断当前获取到的收货地址数据的归属
        if (!byAidAddress.getUid().equals(uid)) {
            throw new ServiceException("非法访问");
        }
        //将所有的收货地址设置为非默认
        Integer i = addressMapper.updateNonDefault(uid);
        if (i < 1) {
            throw new ServiceException("设置默认异常");
        }
        i = addressMapper.updateIsDefaultByAid(aid, username, new Date());
        if (i != 1) {
            throw new ServiceException("更新异常");
        }
    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address byAidAddress = addressMapper.findByAidAddress(aid);
        if (byAidAddress == null) {
            throw new ServiceException("数据不存在");
        }
        if (!byAidAddress.getUid().equals(uid)) {
            throw new ServiceException("非法访问");
        }

        Integer i = addressMapper.deleteByAidInteger(aid);
        if (i != 1) {
            throw new ServiceException("删除异常");
        }
        Integer count = addressMapper.countByUid(uid);
        if (count != 0) {
            Address lastModified = addressMapper.findLastModified(uid);
            if (byAidAddress.getIsDefault() == 1) {
                lastModified.setIsDefault(1);
                Integer rows = addressMapper.updateIsDefaultByAid(lastModified.getAid(), username, new Date());
                if (rows != 1) {
                    throw new ServiceException("更新数据时产生异常");
                }
            }
        }
    }

    @Override
    public Address getByAid(Integer aid, Integer uid) {
        Address address = addressMapper.findByAidAddress(aid);
        if (address == null) {
            throw new ServiceException("收货地址异常");
        }
        if (!address.getUid().equals(uid)) {
            throw new ServiceException("非法访问");
        }
        address.setProvinceCode(null);
        address.setAreaCode(null);
        address.setProvinceCode(null);
        address.setCreatedTime(null);
        address.setCreatedUser(null);
        address.setModifiedTime(null);
        address.setModifiedUser(null);
        return address;
    }
}
